package com.study.channel;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/9/27 15:11
 * channel本身不存数据，需要配合缓冲区进行传输
 */
public class TestChannel
{
    public static final String PATH = "D:\\jdk文档\\1.txt";
    public static void main(String[] args) throws IOException
    {
        copy();
    }

    public static void copy() throws IOException
    {
        FileChannel inChannel = FileChannel.open(Paths.get(PATH), StandardOpenOption.READ);
        FileChannel osChannel = FileChannel.open(Paths.get("D:\\jdk文档\\2.txt"), StandardOpenOption.READ,StandardOpenOption.WRITE,StandardOpenOption.CREATE);
        inChannel.transferTo(0,inChannel.size(),osChannel);
        inChannel.close();
        osChannel.close();
    }

    /**
     * @Description: 分散读取，聚集写入
     * @Param:
     * @return:
     * @Author: 朝花不迟暮
     * @Date: 2020/9/27
     */
    public static void readAndWrite()
    {
        RandomAccessFile rw = null;
        RandomAccessFile rw1 = null;
        try
        {
            rw = new RandomAccessFile("E:\\JavaSpace\\chapterspace\\Character4\\NIO\\test.txt", "rw");
            FileChannel channel = rw.getChannel();
            ByteBuffer buffer1 = ByteBuffer.allocate(100);
            ByteBuffer buffer2 = ByteBuffer.allocate(1024);
            ByteBuffer[] bus = {buffer1, buffer2};
            channel.read(bus);
            for (ByteBuffer byteBuffer : bus)
            {
                byteBuffer.flip();
            }
            System.out.println(new String(bus[0].array(), 0, bus[0].limit()));
            System.out.println(new String(bus[1].array(), 0, bus[1].limit()));
            rw1 = new RandomAccessFile("E:\\JavaSpace\\chapterspace\\Character4\\NIO\\2.txt", "rw");
            FileChannel channel1 = rw1.getChannel();
            channel1.write(bus);
            buffer1.clear();
            buffer2.clear();
        } catch (IOException e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                rw.close();
                rw1.close();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    /**
     * @Description: 也是使用直接缓存方式
     * @Param:
     * @return:
     * @Author: 朝花不迟暮
     * @Date: 2020/9/27
     */
    public static void copyImgUseTrans() throws IOException
    {
        FileChannel inChannel = FileChannel.open(Paths.get("E:\\GoogleDownLoad\\timg.jpg"), StandardOpenOption.READ);
        FileChannel osChannel = FileChannel.open(Paths.get("E:\\GoogleDownLoad\\timg2.jpg"), StandardOpenOption.READ, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
        //inChannel.transferTo(0,inChannel.size(),osChannel);
        osChannel.transferFrom(inChannel, 0, inChannel.size());
        inChannel.close();
        osChannel.close();
    }

    /**
     * @Description: 内存映射文件
     * @Param:
     * @return:
     * @Author: 朝花不迟暮
     * @Date: 2020/9/27
     */
    public static void copyImgUseChannel() throws IOException
    {
        FileChannel inChannel = FileChannel.open(Paths.get("E:\\GoogleDownLoad\\timg.jpg"), StandardOpenOption.READ);
        FileChannel osChannel = FileChannel.open(Paths.get("E:\\GoogleDownLoad\\timg2.jpg"), StandardOpenOption.READ, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
        MappedByteBuffer inMap = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
        MappedByteBuffer osMap = osChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());
        byte[] src = new byte[inMap.limit()];
        inMap.get(src);
        osMap.put(src);
        inChannel.close();
        osChannel.close();
    }

    /**
     * @Description: 需要通过缓存区
     * @Param:
     * @return:
     * @Author: 朝花不迟暮
     * @Date: 2020/9/27
     */
    public static void copyImage() throws IOException
    {

        FileInputStream fis = null;
        FileOutputStream fos = null;
        FileChannel inChannel = null;
        FileChannel osChannel = null;
        try
        {
            fis = new FileInputStream(new File("E:\\GoogleDownLoad\\timg.jpg"));
            fos = new FileOutputStream(new File("E:\\GoogleDownLoad\\timg2.jpg"));
            inChannel = fis.getChannel();
            osChannel = fos.getChannel();
            //分配缓冲区
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            while (inChannel.read(buffer) != -1)
            {
                buffer.flip();
                osChannel.write(buffer);
                buffer.clear();
            }
        } catch (IOException e)
        {
            e.printStackTrace();
        } finally
        {
            osChannel.close();
            inChannel.close();
            fos.close();
            fis.close();
        }
    }
}
